package Classes.GameViews.ViewPanels
{
	import Classes.GameDatas.GD_Building_Func;
	import Classes.GameDatas.GD_Player;
	import Classes.GameDatas.GD_Weapon;
	import Classes.GameUtils.GU_Sound;
	import Classes.GameUtils.GU_dealPower;
	import Classes.GameUtils.GU_dealWeapon;
	import Classes.GameUtils.HavenWall_deal;
	import Classes.GameViews.ViewScenes.ViewScene;
	import Classes.Managers.M_Communication;
	import Classes.staticinfo.Language;
	import Classes.staticinfo.StaticInfo;
	
	import UIComponents.FlatInfo;
	import UIComponents.IUseItem;
	
	import fl.events.ComponentEvent;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.system.ApplicationDomain;
	
	public class VP_Make extends ViewPanel
	{
		private static var _instance:VP_Make;
		private var _interfaceUI:Sprite;
		private var _buildId:Number;
		private var _upBulidObj:Object;
		
		public var preOperate:String;
		/**
		 * 玩家已经装备好的武器 
		 */		
		//		public var haveZBWeapon:Array;
		
		public function VP_Make()
		{
			this.init();
		}
		
		private function init():void
		{
			this._interfaceUI=new (ApplicationDomain.currentDomain.getDefinition("UIMake_v") as Class);
			this.addChild(this.interfaceUI);
			this.addListener();
			this.addObserveFuncs();
		}
		
		private function addObserveFuncs():void
		{
			this.addObserveFunc(GD_Player.instance,StaticInfo.COLLECT_ITEM_CHANGE,refeshCollectItem,false);
			this.addObserveFunc(GD_Player.instance,StaticInfo.CLOSE_EXPEND_LANDPANEL,close,false);
			this.addObserveFunc(GD_Player.instance,StaticInfo.WEAPON_NUMBER_CHANGE,showWeapon,true);
		}
		
		/**
		 * 刷新收藏物品 
		 */		
		private function refeshCollectItem():void
		{
//			var tmpArr:Array=GD_Player.instance.collectMissionArr(this.buildId,this.buildLV);
			this.interfaceUI["setData"](GD_Player.instance.collectMissionArr(this.buildId,this.buildLV));//this.buildId  
			if(this.upBulidObj)
			{
				this.interfaceUI["setupData"](GD_Player.instance.collectMissionArr(this.upBulidObj.taskType),this.upBulidObj);
			}else
			{
				setMaxLvOfBuildInfo();
			}
		}
		
		private function refeshExpendLand():void
		{
			this.interfaceUI["setWallData"](HavenWall_deal.instance.getLandInfoByLandId(GD_Player.instance.wallLv));//围墙等级
			if(this.upBulidObj)
			{
				this.interfaceUI["setupData"](GD_Player.instance.collectMissionArr(this.upBulidObj.taskType),this.upBulidObj);
			}else{
				setMaxLvOfBuildInfo();
			}
		}
		
		private function showWeapon():void
		{
//			var isHome:Boolean=MMT_Character.instance.mapGrid.isHaven;
			var isHome:Boolean=false;
			if(GD_Player.instance.character.view.mapGrid.isHaven || GD_Player.instance.character.view.mapGrid.isWall)
			{
				isHome=true;
			}
			var oo:Object=GD_Player.instance.weapons;
			var ooo:Object=GD_Player.instance.selectWeapons;
			this.interfaceUI["setWeaponData"](GD_Player.instance.weapons,GD_Player.instance.selectWeapons,this.buildLV,isHome);
		}
		
		
		/**
		 *  注册事件 
		 * 
		 */		
		private function addListener():void
		{
			this.addEventListener("close",closeHandler);
			
			this.interfaceUI["zhizBtn"].addEventListener(ComponentEvent.BUTTON_DOWN,clickMKFun);
			this.interfaceUI["upBtn"].addEventListener(ComponentEvent.BUTTON_DOWN,clickUPFun);
			this.interfaceUI["kdan"].addEventListener(ComponentEvent.BUTTON_DOWN,clickLandFun);
			this.interfaceUI["wuButton"].addEventListener(ComponentEvent.BUTTON_DOWN,clickWqFun);
			this.addEventListener("notInHome",notInHomefun);
			this.addEventListener("button_use",function(e:Event):void{GU_Sound.instance.playCommSound("Button_Building_Made");});
			this.addEventListener("addarrowtodj",addarrowtodj);
			this.addEventListener("zbsdld",zbsdld);
			this.addEventListener("notnewguidesd",notnewguidesd);
			
			this.addEventListener("selectLlessThanBuildLV",function(e:Event):void{
				VP_errorPanel.instance.setErrorMessage(Language.selectWeaponslessThanBuildLV);
				VP_errorPanel.instance.enterFrame();
			});
			//			
			//			this.addEventListener(Event.RENDER,onSourceLoadOver);
		}
		
		private function addarrowtodj(e:Event):void
		{
			GU_dealPower.instance.clickSDld();
		}
		
		
		private function zbsdld(e:Event):void
		{
			GU_dealPower.instance.clickWqkClose();
		}
		
		
		private function notnewguidesd(e:Event):void
		{
			VP_errorPanel.instance.setErrorMessage(Language.noOperatePower);
			VP_errorPanel.instance.enterFrame();
		}
		
		private function notInHomefun(e:Event):void
		{
			VP_errorPanel.instance.setErrorMessage(Language.notInHome);
			VP_errorPanel.instance.enterFrame();
			GU_Sound.instance.playCommSound("System_Prompt");	
		}
		private function onSourceLoadOver(e:Event):void
		{
			refeshCollectItem();
			interfaceUI["mkList"]["reshow"]();
		}
		private function clickMKFun(e:ComponentEvent):void
		{
			this.preOperate="MK";
			refeshCollectItem();
			this.contorlView(true,false,false,false);
			interfaceUI["mkList"]["reshow"]();
			if(this.buildId==40102001)
			{
				GU_dealPower.instance.clicksdHc();
			}
			
		}
		
		private function clickUPFun(e:ComponentEvent):void
		{
			var tmpVar:String=this.preOperate;
			this.preOperate="UP";
			if(tmpVar=="WQ")
			{
				sendSaveWeaponMsg();
				return ;
			}
			
			if(!this.upBulidObj)
			{
				setMaxLvOfBuildInfo();
			}else{
				refeshCollectItem();
			}
			this.contorlView(false,true,false,false);
			if(this.buildId==40106001)
			{
				GU_dealPower.instance.clickUpbuilidBtn();
			}
			
			if(FlatInfo.newGuiderQHWq)
			{
				GU_dealPower.instance.removePartArrowForWenpon();
			}
		}
		
		private function setMaxLvOfBuildInfo():void
		{
			var tmpObj:Object=GD_Building_Func.buildingsConfigObj[this.buildId].lvData[this.buildLV];
			this.interfaceUI["setMaxInfo"](tmpObj.name,tmpObj.BLV);
		}
		
		private function clickLandFun(e:ComponentEvent):void
		{
			this.preOperate="Land";
			refeshExpendLand();
			this.contorlView(false,false,true,false);
		}
		
		private function clickWqFun(e:ComponentEvent):void
		{
			if(!GU_dealPower.instance.isHavePower(10000208))
			{
				VP_errorPanel.instance.noPowerMsg();;
				return;
			}
			this.preOperate="WQ";
			showWeapon();
			this.contorlView(false,false,false,true);
//			GU_dealPower.instance.clickDJBtn();
		}
		
		
		/**
		 * 控制页面的隐藏显示  
		 * @param mkPage    加工页面
		 * @param upPage	升级页面
		 * @param kdPage	拓展围墙页面	
		 * @param wppage	武器页面
		 */				
		private function contorlView(mkPage:Boolean=true,upPage:Boolean=false,kdPage:Boolean=false,wppage:Boolean=false):void
		{
			this.interfaceUI["mkList"].visible=mkPage;
			this.interfaceUI["upLIst"].visible=upPage;
			this.interfaceUI["kdList"].visible=kdPage;
			this.interfaceUI["weaponList"].visible=wppage;
			this.interfaceUI["zhizBtn"]["lockState"]=mkPage;
			this.interfaceUI["upBtn"]["lockState"]=upPage;
			this.interfaceUI["kdan"]["lockState"]=kdPage;
			this.interfaceUI["wuButton"]["lockState"]=wppage;
		}
		
		/**
		 * 控制按钮的显示 
		 * @param mkBtn  加工按钮
		 * @param upBtn  升级按钮
		 * @param kdBtn  拓展围墙按钮	
		 * @param wpBtn  武器按钮
		 * 
		 */		
		private function controlBtn(mkBtn:Boolean=true,upBtn:Boolean=false,kdBtn:Boolean=false,wpBtn:Boolean=false):void
		{
			this.interfaceUI["zhizBtn"].visible=mkBtn;
			this.interfaceUI["upBtn"].visible=upBtn;
			this.interfaceUI["kdan"].visible=kdBtn;
			this.interfaceUI["wuButton"].visible=wpBtn;
		}
		
		private function closeHandler(e:Event):void
		{
			GU_dealPower.instance.removePartArrowForWenpon();
			if(this.preOperate=="WQ")
			{
				sendSaveWeaponMsg();
			}else{
				this.close();
			}
			this.removeEventListener("sourceLoadOver",onSourceLoadOver);
			
			if(GU_dealPower.instance.isHavePower(10000208) && null!=GD_Player.instance.missions[11000006])
			{
				GU_dealPower.instance.clickChangeWeapon();
			}
		}
		
		
		/**
		 * 向后台发送保存玩家武器装备的命令 
		 */		
		private function sendSaveWeaponMsg():void
		{
			//如果数据没有进行改动 ，则不像后台发包
			if(!filterNewWeapons())
			{
				resetWeaponsFun();
				return;
			}
			var newWeaponArr:Array=this.interfaceUI["newWeaponsArr"] as Array;
			if(null!=newWeaponArr && newWeaponArr.length>0)
			{
				//发送向后保存装备好武器的事件
				var ids:Array=new Array();
				for(var i:int=0;i<newWeaponArr.length;i++)
				{
					var tmpItem:IUseItem=newWeaponArr[i] as IUseItem;
					ids.push(tmpItem.itemId);
				}
				var userWeaponsId:String=ids.join(",");
				M_Communication.instance.send(12701,{allWeaponID:userWeaponsId});
				this.resetWeaponsFun();
			}else{
				/**
				 * 弹框提示，你不能不携带武器出门，离开将取消本次武器装备操作！
				 * 如果点击确定  放弃本次操作  数据就是以前的那个,关闭弹框 ;如果点击取消  则关闭弹框 回到选择武器的页面
				 * */
				VP_TextWinPanel.instance.openTextWindon("",Language.sureCancle,resetWeaponsFun);
			}
		}
		
		/**
		 * 更新装备区域的数据 
		 * 
		 */		
		
		public function freshWeaponData():void
		{
			GD_Player.instance.selectWeapons=[];
			GD_Player.instance.weapons=[];
			var newWeaponArr:Array=this.interfaceUI["newWeaponsArr"] as Array;
			var tmpSelectObj:Object=new Object();
			for(var j:int=0;j<newWeaponArr.length;j++)
			{
				var tmpItems:IUseItem=newWeaponArr[j] as IUseItem;
				tmpSelectObj[tmpItems.itemId]=tmpItems as GD_Weapon;
			}
			GD_Player.instance.selectWeapons=tmpSelectObj;
			var allWeaponsObj:Object=new Object();
			var allWeapons:Array=this.interfaceUI["allWeapons"] as Array;
			var len:int=allWeapons.length;
			for(var i:int=0;i<len;i++)
			{
				var tmpItem:IUseItem=allWeapons[i] as IUseItem;
				allWeaponsObj[tmpItem.itemId]=tmpItem as GD_Weapon;
			}
			GD_Player.instance.weapons=allWeaponsObj;
		}
		
		/**
		 * 重置已经装备好的武器数据 
		 */		
		private function resetWeaponsFun():void
		{
			//当点击确定按钮时,如果点击的是 升级 则切换到升级的页面      如果点击关闭按钮则关闭页面
			if(this.preOperate=="UP")
			{
				this.interfaceUI["upBtn"].dispatchEvent(new ComponentEvent(ComponentEvent.BUTTON_DOWN));
			}else{
				this.close();
			}
			
		}
		
		/**
		 * 把新装备的数据 跟原来是装备数据进行对比 ，是否重新装备了武器
		 */		
		private function filterNewWeapons():Boolean
		{
			var newWeaponArr:Array=this.interfaceUI["newWeaponsArr"] as Array;
			var tmpWeapons:Object=GD_Player.instance.selectWeapons;
			var tempArr:Array=new Array();
			var flag:Boolean=false;
			for each (var weapon:IUseItem in tmpWeapons)
			{
				tempArr.push(weapon);
			}
			
			if(tempArr.length!=newWeaponArr.length)
			{
				flag=true;
			}else{
				for(var i:int=0;i<tempArr.length;i++)
				{
					if((tempArr[i] as IUseItem).itemId != (newWeaponArr[i] as IUseItem).itemId)
					{
						flag=true;
						break;
					}
				}
			}
			return flag;
		}
		
		/**
		 * 是否是中心建筑 
		 */		
		private var cenerBuildFlag:Boolean=false;
		private var buildLV:Number;
		public function openBuildWin(vs:ViewScene,buildId:Number,buildObj:Object,buildLv:Number):void
		{
			/**主建筑逻辑*/
			var isShow:Boolean	=	false;
			this._buildId=buildId;
			this.buildLV=buildLv;
			this.upBulidObj=buildObj;
			if(this._buildId==40101001)
			{
				this.interfaceUI["setTitle"]("避难所基地");
				cenerBuildFlag=true;
				
				this.controlBtn(false,true,true,false);
				this.interfaceUI["kdan"].dispatchEvent(new ComponentEvent(ComponentEvent.BUTTON_DOWN));
				
			}else if(this._buildId==40106001){ //40106001  40105001
				/**武器建筑逻辑*/
				this.interfaceUI["setTitle"]("武器更换");
				cenerBuildFlag=false;
				this.controlBtn(false,true,false,true);
				//new guide
				if(GU_dealPower.instance.isHavePower(10000211) && !GU_dealPower.instance.isHavePower(10000208))
				{
					this.interfaceUI["upBtn"].dispatchEvent(new ComponentEvent(ComponentEvent.BUTTON_DOWN));
				}else{
					this.interfaceUI["wuButton"].dispatchEvent(new ComponentEvent(ComponentEvent.BUTTON_DOWN));
				}
				GU_dealWeapon.instance.removeTxToWeaponbuild();
			}else{
				/**非主建筑逻辑*/
				this.interfaceUI["setTitle"]("制造基地");
				cenerBuildFlag=false;
				this.controlBtn(true,true,false,false);
				isShow=true;
				this.interfaceUI["zhizBtn"].dispatchEvent(new ComponentEvent(ComponentEvent.BUTTON_DOWN));
			}
			openAt(vs);
			if(isShow){
				this.addEventListener("sourceLoadOver",onSourceLoadOver);
				interfaceUI["mkList"]["reshow"]();
			}
			
			GU_dealPower.instance.newGuideWeapon();
			
			//武器库 升级
			if(GU_dealPower.instance.isHavePower(10000211) && null!=GD_Player.instance.missions[11000005] && buildId==40106001)
			{
				GU_dealPower.instance.clickwqksj();
			}
			
			//中心建筑
			if(buildId==40101001)
			{
				GU_dealPower.instance.clickCK();
			}
			
		}
		
		
		
		override public function openAt(vs:ViewScene,bg:Boolean=false,sd:Boolean=true):void
		{
			super.openAt(vs,true,sd);
			this.toCenter();
		}
		
		/*getter & setter*/
		public static function get instance():VP_Make
		{
			if(!_instance)_instance=new VP_Make;
			return _instance;
		}
		
		public function get interfaceUI():Sprite
		{
			return _interfaceUI;
		}
		
		/**
		 * 建筑ID 
		 */
		public function get buildId():Number
		{
			return _buildId;
		}
		
		
		/**
		 * @private
		 */
		public function set buildId(value:Number):void
		{
			_buildId = value;
		}
		
		/**
		 * 建筑升级之后的数据类型 
		 */
		public function get upBulidObj():Object
		{
			return _upBulidObj;
		}
		
		/**
		 * @private
		 */
		public function set upBulidObj(value:Object):void
		{
			_upBulidObj = value;
		}
		
		
	}
}